home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / ccmd / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-19  |  2.5 KB  |  84 lines

  1. /*
  2.  Author: Andrew Lowry
  3.  
  4.  Columbia University Center for Computing Activities, July 1986.
  5.  Copyright (C) 1986, 1987, Trustees of Columbia University in the City
  6.  of New York.  Permission is granted to any individual or institution
  7.  to use, copy, or redistribute this software so long as it is not sold
  8.  for profit, provided this copyright notice is retained.
  9. */
  10. /* This file reads from standard input and distributes what it reads
  11. ** to output files as directed by the input stream.  Distribution is
  12. ** signaled by lines of the form:
  13. **
  14. **     #file xxx
  15. **
  16. ** When such a line is encountered, the current output file is closed
  17. ** and the file named xxx is opened and made the current output file.
  18. ** The line containing the "#file" marker is not output to any file.
  19. **/
  20.  
  21. #include <stdio.h>
  22.  
  23. /* miscellaneous definitions */
  24.  
  25. #define TRUE -1
  26. #define FALSE 0
  27.  
  28. #define NULCHAR '\000'
  29. #define TAB '\011'
  30. #define NEWLINE '\n'
  31. #define SPACE '\040'
  32.  
  33. #define BUFSIZE 100
  34.  
  35. char inbuf[BUFSIZE];
  36.  
  37. FILE *out = stdout;
  38.  
  39. main()
  40. {
  41.   while (fgets(inbuf, BUFSIZE, stdin) != NULL) /* read until EOF */
  42.     if (!chkfile())        /* check each line for #file */
  43.       fputs(inbuf,out);        /* write other lines */
  44.   fclose(out);            /* close up most recent output file */
  45. }
  46.  
  47. int
  48. chkfile()
  49. {
  50.   char *test = "#FILE";        /* this is what we look for */
  51.   char *fname;
  52.   char tc,ic;            /* chars from test and input strings */
  53.   int i;
  54.  
  55.   for (i = 0; (tc = test[i]) != NULCHAR; i++) { /* loop through test string */
  56.     ic = inbuf[i];        /* get next input char */
  57.     if ((ic >= 'a') && (ic <= 'z')) /* convert to upper case */
  58.       ic -= 'a'-'A';
  59.     if (tc != ic)        /* chars don't match? */
  60.       return(FALSE);        /* then not a file line */
  61.   }
  62.   if ((inbuf[i] != SPACE) && (inbuf[i] != TAB))
  63.     return(FALSE);        /* must be followed by whitespace */
  64.   else
  65.     i++;
  66.   while ((inbuf[i] == SPACE) || (inbuf[i] == TAB))
  67.     i++;            /* skip remaining whitespace */
  68.   fname = &inbuf[i];        /* point at beginning of filename */
  69.   while (inbuf[i] != NULCHAR)    /* search for and remove newline or space */
  70.     if ((inbuf[i] == NEWLINE) || (inbuf[i] == SPACE) || (inbuf[i] == TAB))
  71.       inbuf[i] = NULCHAR;    /* replace it with null char */
  72.     else
  73.       i++;
  74.   fprintf(stderr,"*** Opening %s for output\n",fname);
  75.   if (out != stdout)
  76.     fclose(out);        /* close previous output file */
  77.   out = fopen(fname,"w");    /* open up specified file */
  78.   if (out == NULL) {
  79.     fprintf(stderr,"*** Could not open %s, using standard out\n",fname);
  80.     out = stdout;        /* use stdout if open fails */
  81.   }
  82.   return(TRUE);            /* tell them not to output this line */
  83. }
  84.